// pages/travel/list.js
import { GetPlaceListForPage,getItineraryListForPage,getItineraryListForPageForUser } from '../api/home'
import { imageURL } from '../../utils/request'
Page({
  data: {
    myItinerary:{},
    activeTab: 0,
    showAddPopup:false,
    imageURL:'',
    playList: [],
    interestingList: [],
    pagePlay: 1,
    pageInteresting: 1,
    rows: 10,
    loadingPlay: false,
    loadingInteresting: false,
    hasMorePlay: true,
    hasMoreInteresting: true
  },
  onLoad(){
    this.setData({imageURL})
    this.getMyList()
    this.getPlay()
    this.Interesting()
  },
  async getMyList(){
    const res = await getItineraryListForPageForUser()
    this.setData({
      myItinerary:res.data.list[0]
    })
  },
  goMyDetail(){
    wx.navigateTo({
      url: `/pages/Itinerary/myItineraryDetails/myItineraryDetails?itinerary_id=${this.data.myItinerary.itinerary_id}`,
    })
  },
  // 获取热门景点
  async getPlay() {
    if (!this.data.hasMorePlay || this.data.loadingPlay) return;

    this.setData({ loadingPlay: true });

    const res = await GetPlaceListForPage({
      page: this.data.pagePlay,
      rows: this.data.rows,
      // is_hot: 1
    });

    const newList = res.data.list || [];
    this.setData({
      playList: [...this.data.playList, ...newList],
      pagePlay: this.data.pagePlay + 1,
      hasMorePlay: newList.length === this.data.rows,
      loadingPlay: false
    });
  },

  // 获取有趣行程
  async Interesting() {
    if (!this.data.hasMoreInteresting || this.data.loadingInteresting) return;

    this.setData({ loadingInteresting: true });

    const res = await getItineraryListForPage({
      page: this.data.pageInteresting,
      rows: this.data.rows,
      // is_recommend: 1
    });

    const newList = res.data.list || [];
    this.setData({
      interestingList: [...this.data.interestingList, ...newList],
      pageInteresting: this.data.pageInteresting + 1,
      hasMoreInteresting: newList.length === this.data.rows,
      loadingInteresting: false
    });
  },

  // 切换 Tab
  switchTab(e) {
    const index = e.currentTarget.dataset.index;
    if (index === 0) {
      // 重置有趣行程参数
      this.setData({
        interestingList: [],
        pageInteresting: 1,
        hasMoreInteresting: true,
        loadingInteresting: false
      });
      this.Interesting(); // 重新请求
    }
  
    if (index === 1) {
      // 重置热门景点参数
      this.setData({
        playList: [],
        pagePlay: 1,
        hasMorePlay: true,
        loadingPlay: false
      });
      this.getPlay(); // 重新请求
    }
    this.setData({ activeTab: index });
  },
  onReachBottom() {
    if (this.data.activeTab === 0) {
      this.Interesting();
    } else if (this.data.activeTab === 1) {
      this.getPlay();
    }
  },
  establishClick(){
    wx.navigateTo({
      url: '/pages/Itinerary/addItinerary/addItinerary',
    })
  },
  openPopup() {
    this.setData({ showAddPopup: true });
  },
  onConfirmCollect(e) {
    console.log("選中的收藏夾：", e.detail.folder);
  },
  goMy(){
    wx.navigateTo({
      url: '/pages/Itinerary/myLtinerary/myLtinerary',
    })
  },
  goList(){
    const index = this.data.activeTab
    if(index==0){
      wx.navigateTo({
        url: `/pages/Itinerary/ItineraryList/ItineraryList`,
      })
    }else{
      wx.navigateTo({
        url: `/pages/Itinerary/hotList/hotList`,
      })
    }
  },
  goDetails(e){
    const place_id = e.currentTarget.dataset.place_id
    wx.navigateTo({
      url: `/pages/Itinerary/SceneryDetails/SceneryDetails?place_id=${place_id}`,
    })
  },
  goItineraryDetails(e){
    const itinerary_id = e.currentTarget.dataset.itinerary_id
    wx.navigateTo({
      url: `/pages/Itinerary/ItineraryDetails/ItineraryDetails?itinerary_id=${itinerary_id}`,
    })
  },
})